#include "operasqlite.h"

operaSqlite::operaSqlite() {}

void operaSqlite::setContain(QString contain) { sqlContain = contain; }

operaSqlite::~operaSqlite() { closeSqlite(); }

bool operaSqlite::openSqlite(QString myDb, QString &error) {
  bool bRet = false;
  do {
    if (myDb.isEmpty()) {
      break;
    }

    if (QSqlDatabase::contains(sqlContain)) {
      sqliteDb = QSqlDatabase::database(sqlContain);
      if (false == sqliteDb.isOpen()) {
        sqliteDb.open();
      } else {
        error = QString("数据库打开错误:%1").arg(sqliteDb.lastError().text());
      }
    } else {
      sqliteDb = QSqlDatabase::addDatabase("QSQLITE", sqlContain);
      sqliteDb.setDatabaseName(myDb);
      if (false == sqliteDb.open("root", "Smaller")) {
        error = QString("数据库打开错误:%1").arg(sqliteDb.lastError().text());
        break;
      }
    }
    bRet = true;
  } while (0);

  return bRet;
}

bool operaSqlite::closeSqlite() {
  bool bRet = false;
  QSqlDatabase db;
  do {
    if (QSqlDatabase::contains(sqlContain)) {
      db = QSqlDatabase::database(sqlContain);
      if (true == db.isOpen()) {
        db.close();
      }
    }
    bRet = true;
  } while (0);
  return bRet;
}

QStringList operaSqlite::getTableInfo(QString tabName) {
  QStringList nameList;
  QSqlQuery query(sqliteDb);
  QString getTableInfo = "PRAGMA table_info(" + tabName + ")";

  query.prepare(getTableInfo);

  if (query.exec()) {
    while (query.next()) {
      nameList.append(
          query.value(query.record().indexOf(QString("name"))).toString());
    }
  } else {
    qDebug() << query.lastError();
  }

  return nameList;
}

void operaSqlite::openTransaction() { sqliteDb.transaction(); }
void operaSqlite::closeTransaction() { sqliteDb.commit(); }

void operaSqlite::write(const QString &data) {
  QSqlQuery query(sqliteDb);
  if (false == query.exec(data)) {
    qDebug() << query.lastError().text();
  }
}

void operaSqlite::clearSqlite(QString &tableName) {
  QString execSql = clearSeq + tableName;
  do {
    QSqlQuery query(sqliteDb);

    if (false == query.exec(execSql)) {
      qDebug() << query.lastError().text();
      break;
    }

    execSql = QString("select * from sqlite_sequence");
    if (false == query.exec(execSql)) {
      qDebug() << query.lastError().text();
      break;
    }

    execSql = QString("update sqlite_sequence set seq=0 where name='%1'")
                  .arg(tableName);
    if (false == query.exec(execSql)) {
      qDebug() << query.lastError().text();
      break;
    }

    execSql = QString("vacuum");
    if (false == query.exec(execSql)) {
      qDebug() << query.lastError().text();
      break;
    }
  } while (0);
}

bool operaSqlite::isOpen() { return sqliteDb.isOpen(); }

QSqlDatabase operaSqlite::getDb() { return sqliteDb; }

bool operaSqlite::reconTable(const QString &tableName,
                             const QStringList &head) {
  bool bRet = false;
  do {
    QString execSql = QString("PRAGMA foreign_keys = 0");
    QSqlQuery query(sqliteDb);

    if (false == query.exec(execSql)) {
      qDebug() << query.lastError().text();
      break;
    }
    execSql = QString("DROP TABLE %1").arg(tableName);
    if (false == query.exec(execSql)) {
      qDebug() << query.lastError().text();
      break;
    }
    /* 建表 */
    execSql =
        QString(
            "CREATE TABLE %1 ( ID INTEGER UNIQUE  PRIMARY KEY AUTOINCREMENT, ")
            .arg(tableName);

    QString tmpSql;

    if (head.isEmpty())
      break;

    for (int hCnt = 0; hCnt < head.count(); hCnt++) {
      if (hCnt == head.count() - 1) {
        execSql += QString("%1 TEXT )")
                       .arg(QString(head.at(hCnt)).replace(QRegExp("/"), "_"));
        continue;
      }
      execSql += QString("%1 TEXT,")
                     .arg(QString(head.at(hCnt)).replace(QRegExp("/"), "_"));
    }

    if (false == query.exec(execSql)) {
      qDebug() << query.lastError().text();
      break;
    }

  } while (0);

  return bRet;
}
